<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model.trim="paramsQuery.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="onSearch" />
        <el-tree
          ref="deptTree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="selectNode"
        />
      </div>

      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>

          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>

          <el-button size="mini" @click="exportEmployee">excel导出</el-button>

        </el-row>
        <el-table :data="userList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template #default="{row}">
              <img v-if="row.staffPhoto" :src="row.staffPhoto" alt="" class="avatar">
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span v-if="row.formOfEmployment===1">正式</span>
              <span v-else-if="row.formOfEmployment===2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="openEnableRoleAList(row.id)">角色</el-button>

              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">编辑</el-button>

              <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>

            </template>

          </el-table-column>

        </el-table>
        <el-pagination
          layout="total, prev, pager, next"
          :total="total"
          :current-page="paramsQuery.page"
          :page-size="paramsQuery.pagesize"
          @current-change="pageChange"
        />
      </div>
      <ImportExcel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="getDepartment" />
      <el-dialog :visible.sync="showRoleDialog" title="分配角色">
        <el-checkbox-group v-model="roleIds">
          <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id" :value="item.name">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
        <el-button size="mini" style="margin-right: 30px;" @click="btnOk">确定</el-button>
        <el-button size="mini" type="primary" @click="btnCancel">取消</el-button>
      </el-dialog>

    </div>

  </div>

</template>

<script>
import { getDepartmentAPI } from '@/api/department'
import { delEmployeeAPI, exportEmployeeAPI, getUserAPI, getUserInfoAPI } from '@/api/employee'
import { transObjToTree } from '@/utils'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'
import { Message } from 'element-ui'
import { editEnableRoleListAPI, getEnableRoleListAPI } from '@/api/role'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      showRoleDialog: false,
      roleList: [],
      showExcelDialog: false,
      userList: [],
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      paramsQuery: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      total: 0,
      currentRoleid: 0,
      roleIds: []
    }
  },
  mounted() {
    this.getDepartment()
  },
  methods: {
    btnCancel() {
      this.roleIds = []
      this.showRoleDialog = false
    },
    async btnOk() {
      await editEnableRoleListAPI({ id: this.currentRoleid, roleIds: this.roleIds })
      Message.success('修改员工分配角色成功')
    },
    async openEnableRoleAList(id) {
      this.showRoleDialog = true
      this.currentRoleid = id
      this.roleList = await getEnableRoleListAPI()
      const { roleIds } = await getUserInfoAPI(id)
      this.roleIds = roleIds
    },
    del(id) {
      this.$confirm('确定要删除该员工信息吗', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeAPI(id)
        if (this.paramsQuery.pagesize === 1 && this.paramsQuery.page > 1) {
          this.paramsQuery.page -= 1
        }
        this.getUserList()
        Message.success('删除员工成功')
      })
    },
    async exportEmployee() {
      const res = await exportEmployeeAPI()
      FileSaver.saveAs(res, '员工信息表xlsx')
    },
    async getDepartment() {
      const res = await getDepartmentAPI()
      this.depts = transObjToTree(res, 0)
      this.paramsQuery.departmentId = this.depts[0].id
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.paramsQuery.departmentId)
      })
      this.getUserList()
    },
    selectNode(node) {
      this.paramsQuery.departmentId = node.id
      this.paramsQuery.page = 1
      this.getUserList()
    },
    async getUserList() {
      const res = await getUserAPI(this.paramsQuery)
      this.userList = res.rows
      this.total = res.total
    },
    async pageChange(val) {
      this.paramsQuery.page = val
      await this.getUserList()
    },
    onSearch() {
      clearTimeout(this.timeid)
      this.timeid = setTimeout(() => {
        this.paramsQuery.page = 1
        this.getUserList()
      }, 500)
    }
  }
}
</script>

    <style lang="scss" scoped>
    .app-container {
    background: #fff;
    display: flex;
    .left {
      width: 280px;
      padding: 20px;
      border-right: 1px solid #eaeef4;
    }
    .right {
      flex: 1;
      padding: 20px;
      .opeate-tools {
        margin:10px ;
      }
      .avatar{
        height: 40px;
        width: 40px;
      }
      .username {
        height: 30px;
        width: 30px;
        line-height: 30px;
        text-align: center;
        border-radius: 50%;
        color: #fff;
        background: #04C9BE;
        font-size: 12px;
        display:inline-block;
      }
    }
  }

</style>
